<?php
	header("Content-Type: text/html;charset=utf-8");
	include("../_judge.php");
	include("../../function/function.php");
	
	$action = chkstr(trim($_REQUEST['action']));
	$news_language = chkstr(trim($_REQUEST['news_language']));
	$news_title = chkstr(trim($_REQUEST['news_title']));
	$news_color = chkstr(trim($_REQUEST['news_color']));
	$news_style = $_REQUEST['news_style'];
	$news_from = chkstr(trim($_REQUEST['news_from']));
	$news_writer = chkstr(trim($_REQUEST['news_writer']));
	$news_keywords = chkstr(trim($_REQUEST['news_keywords']));
	$news_desc = chkstr(trim($_REQUEST['news_desc']));
	$news_bigclass = chkstr(trim($_REQUEST['news_bigclass']));
	$news_pic = chkstr(trim($_REQUEST['news_pic']));
	$news_tourls = chkstr(trim($_REQUEST['news_tourls']));
	$news_order = $_REQUEST['news_order'];
	$news_text = chkstr(trim($_REQUEST['news_text']));
	$news_money = chkstr(trim($_REQUEST['news_money']));
	$news_kucun = chkstr(trim($_REQUEST['news_kucun']));
	$news_addtime = chkstr(trim($_REQUEST['news_addtime']));
	$news_content = addslashes($_REQUEST['news_content']);
	$now_time = date("Y-m-d H:i:s");
	$re_url = trim($_REQUEST['re_url']);
	$newsid = $_REQUEST['newsid'];
	$news_option = chkstr(trim($_REQUEST['news_option']));
	$news_picorder = $_REQUEST['news_picorder'];
	$news_pictext = $_REQUEST['news_pictext'];
	$news_picurl = $_REQUEST['news_picurl'];
	$id = trim($_REQUEST['id']);
	
	
	
	if(is_array($news_style)){ // 拆分数组
		$news_style = implode(',',$news_style);
	}
	if(is_array($newsid)){ // 拆分数组
		$newsid = implode(',',$newsid);
	}
	if(empty($news_hit)){
		$news_hit = 0;
	}
	if(empty($news_money)){
		$news_money = 0;
	}
	if(empty($news_kucun)){
		$news_kucun = 0;
	}
	
	
	
	if($action == "add"){
		if(strlen($news_title)<1 || strlen($news_bigclass)<1){
			echo erro("请将信息填写完整");
		}
		if(!is_numeric($news_hit)){
			echo erro("浏览量只能为数字");
		}
		if(!is_numeric($news_kucun)){
			echo erro("库存只能为数字");
		}
		if(!is_numeric($news_money)){
			echo erro("金额只能为数字");
		}
		if($news_money <0){
			echo erro("金额不能为负数");
		}
		

		$NpicCount = count($news_picurl);
		if($NpicCount >0){
			for($i=0; $i<$NpicCount; $i++){
				if(isNumber($news_picorder[$i])){
					$NpicJson[$i]['norder'] = $news_picorder[$i];
				}else{
					echo erro("数字");
					$NpicJson[$i]['norder'] = 1;
				}
				$NpicJson[$i]['ntext'] = $news_pictext[$i];
				$NpicJson[$i]['nurls'] = $news_picurl[$i];
			}
			$news_pic_batch = json_encode($NpicJson);
			$news_pic_batch = str_replace('\\','\\\\',$news_pic_batch);
		}else{
			$news_pic_batch = "";
		}
		
		
		// 添加数据
		$mysql = new sqlstr("kboy_pro"); 
		$mysql -> set("news_language",$news_language,true);
		$mysql -> set("news_title",$news_title);
		$mysql -> set("news_color",$news_color);
		$mysql -> set("news_style",$news_style);
		$mysql -> set("news_from",$news_from);
		$mysql -> set("news_writer",$news_writer);
		$mysql -> set("news_keywords",$news_keywords);
		$mysql -> set("news_desc",$news_desc);
		$mysql -> set("news_bigclass",$news_bigclass,true);
		$mysql -> set("news_pic",$news_pic);
		$mysql -> set("news_pic_batch",$news_pic_batch);
		$mysql -> set("news_tourls",$news_tourls);
		$mysql -> set("news_text",$news_text);
		$mysql -> set("news_content",$news_content);
		$mysql -> set("news_hit",$news_hit,true);
		$mysql -> set("news_kucun",$news_kucun,true);
		$mysql -> set("news_money",$news_money,true);
		$mysql -> set("news_updatetime",$news_addtime);
		$news_sql = $mysql -> insertSql();
		$news_res = mysql_query($news_sql);
		if($news_res){
			$get_id = mysql_insert_id(); //获取最后一条插入的id
			mysql_query("update kboy_pro set news_order=$get_id where id=$get_id");
			echo ok("操作成功","newsadd.php");
		}else{
			echo erro("服务器内部错误");
		}

	}elseif($action == "edit"){
		if(strlen($news_title)<1 || strlen($news_bigclass)<1 || strlen($id)<1){
			echo erro("请将信息填写完整");
		}
		if(!is_numeric($id)){
			echo erro("ID错误");
		}
		if(!is_numeric($news_hit)){
			echo erro("浏览量只能为数字");
		}
		if(!is_numeric($news_kucun)){
			echo erro("库存只能为数字");
		}
		if(!is_numeric($news_money)){
			echo erro("金额只能为数字");
		}
		if($news_money <0){
			echo erro("金额不能为负数");
		}

		$NpicCount = count($news_picurl);
		if($NpicCount >0){
			for($i=0; $i<$NpicCount; $i++){
				if(isNumber($news_picorder[$i])){
					$NpicJson[$i]['norder'] = $news_picorder[$i];
				}else{
					$NpicJson[$i]['norder'] = 1;
				}
				$NpicJson[$i]['ntext'] = $news_pictext[$i];
				$NpicJson[$i]['nurls'] = $news_picurl[$i];
			}
			$news_pic_batch = json_encode($NpicJson);
			$news_pic_batch = str_replace('\\','\\\\',$news_pic_batch);
		}else{
			$news_pic_batch = "";
		}



		// 修改数据
		$mysql = new sqlstr("kboy_pro"); 
		$mysql -> set("news_language",$news_language,true);
		$mysql -> set("news_title",$news_title);
		$mysql -> set("news_color",$news_color);
		$mysql -> set("news_style",$news_style);
		$mysql -> set("news_from",$news_from);
		$mysql -> set("news_writer",$news_writer);
		$mysql -> set("news_keywords",$news_keywords);
		$mysql -> set("news_desc",$news_desc);
		$mysql -> set("news_bigclass",$news_bigclass,true);
		$mysql -> set("news_pic",$news_pic);
		$mysql -> set("news_pic_batch",$news_pic_batch);
		$mysql -> set("news_tourls",$news_tourls);
		$mysql -> set("news_text",$news_text);
		$mysql -> set("news_content",$news_content);
		$mysql -> set("news_hit",$news_hit,true);
		$mysql -> set("news_kucun",$news_kucun,true);
		$mysql -> set("news_money",$news_money,true);
		$mysql -> set("news_updatetime",$now_time);
		$news_sql = $mysql -> updateSql("id=$id");
		$news_res = mysql_query($news_sql);
		if($news_res){
			echo ok("操作成功",$re_url);
		}else{
			echo erro("服务器内部错误");
		}
		
	}elseif($action == "del"){
		if($news_option == "del"){
			if(!empty($newsid)){
				// 删除数据
				$mysql = new sqlstr("kboy_pro"); 
				$news_sql = $mysql -> deleteSql("id in($newsid)");
				$news_res = mysql_query($news_sql);
				if($news_res){
					echo ok("操作成功",$_SERVER['HTTP_REFERER']);
				}else{
					echo erro("服务器内部错误");
				}
			}else{
				echo erro("至少选择一条记录进行操作");
			}
		}else{
			erro("请选择要操作的方式");
		}
	
	}elseif($action == "ztop"){//产品置顶
		if(strlen($id)<1){
			echo erro("ID错误");
		}
		if(!is_numeric($id)){
			echo erro("ID错误");
		}
		
		$mysql = new sqlstr("kboy_pro"); 
		$mysql -> set("news_top",$now_time);
		$news_sql = $mysql -> updateSql("id=$id");
		$news_res = mysql_query($news_sql);
		if($news_res){
			echo re($_SERVER['HTTP_REFERER']);
		}else{
			echo erro("服务器内部错误");
		}
		
	}elseif($action == "ntop"){//取消产品置顶
		if(strlen($id)<1){
			echo erro("ID错误");
		}
		if(!is_numeric($id)){
			echo erro("ID错误");
		}
		$mysql = new sqlstr("kboy_pro"); 
		$mysql -> set("news_top","");
		$news_sql = $mysql -> updateSql("id=$id");
		$news_res = mysql_query($news_sql);
		if($news_res){
			echo re($_SERVER['HTTP_REFERER']);
		}else{
			echo erro("服务器内部错误");
		}
		
	}elseif($action == "ordernum"){//取消产品置顶
		if(strlen($id)<1){
			echo erro("ID错误");
		}
		if(!is_numeric($id)){
			echo erro("ID错误");
		}
		if(!is_numeric($news_order)){
			echo erro("顺位错误");
		}
		if($news_order<=0){
			echo erro("顺位错误");
		}
		$mysql = new sqlstr("kboy_pro"); 
		$mysql -> set("news_order",$news_order);
		$news_sql = $mysql -> updateSql("id=$id");
		$news_res = mysql_query($news_sql);
		if($news_res){
			echo re($_SERVER['HTTP_REFERER']);
		}else{
			echo erro("服务器内部错误");
		}
		
	}else{
		echo erro("参数错误");
	}
	
?>
